/**
 * @Description home-view
 * @author lizhilin
 * @update 2016-08-12
 */
define(function(require, exports, modules) {
	'use strict';
	var template = require('text!./tpl.farm.html'),
        ad=require('text!../farm/ad.html'),
		util = require('util');

	var View = Backbone.View.extend({
		el : '#page',
		initPageData: {},
		userCookie : JSON.parse(util.getCookie('user')),
		events : {
			//页面元素
			'click em.back': function () {
				Backbone.history.navigate('home',{trigger:true});
			},
			'click em.refresh': function () {
				Backbone.history.loadUrl('farm?time='+new Date().getTime());
				//this.RefreshData();
			},
			//弹窗显示临时控制
			'click .chicken-page>.notice em': function () {
				this.show_shade('notice');
				jsonpGet(
					util.NDNserver+'/getNoticeList',
					{
						userId: this.userCookie.userId,
						token: this.userCookie.token
					},
					'getNoticeList',
					$.proxy(function (res) {
						if(res.code == 1){
							$('section.shade .notice ul').empty();
							$.each(res.data, function (index, ele) {
								$('section.shade .notice ul').append(
								'<li>'+
									'<div class="top">'+
										'<div class="name">'+ele.noticeTitle+'</div>' +
										'<time>'+formatDate(new Date(ele.createTime),5)+'</time>'+
									'</div>'+
									'<p>'+ele.noticeContent+'</p>'+
								'</li>'
								);
							});
							$('section.shade .notice ul li').eq(1).addClass('cur');
						}else {
							commonAlert_oneBtn('请求失败');
						}
					},this)
				)
			},
			'click .userBox button.friend': function () {
				this.show_shade('friend-list');
				this.getDate('friendList');
			},
			'click .friend-list #quickClean':'quickClean',
			'click .friend-list #invitation_friend':function () {
				this.show_shade('invite');
			},
			'click .invite':function(event){
				this.hide_shade();
			},
			'click .userBox button.library': 'gotoLibrary',
			'click .library-dog button': 'show_buy_dog',//购买 或 升级小狗
			'click section.shade .UpDog button.yellowBtn':'buy_dog',
			'click section.shade .show_buyDog_prompt button.yellowBtn':'buyDog_prompt',
			'click section.shade .show_buyDog_prompt button.buffBtn':function(){
				this.hide_shade();
			},
			'click section.shade li.haveSuperIncubator button': 'buySuper',
			'click section.shade .buySuper button.buffBtn': 'showNoMchine',
			'click section.shade .buySuper button.yellowBtn': 'buySuper_sure',
			'click .NoChick button': function () { this.hide_shade();},
			'click section.shade .UpDog button.buffBtn': function () { this.show_shade('storage');},
			//元素控制
			'click #gotoUp': function () {//去增养
				$('.desktop button.Up').addClass('cur').siblings().removeClass('cur');
				$('ul.field li').removeClass('canOpen');
				$('ul.field li').each(function (index, ele) {
					if($(this).find('.chicken-box').length != 0){
						$(this).addClass('canUp');
					}
				});
				this.hide_shade();
			},
			'click #hatch': function () {//去孵化
				$('.desktop button.hatch').addClass('cur').siblings().removeClass('cur');
				$('ul.field li').removeClass('canOpen');
				$('ul.field li').each(function (index, ele) {
					if($(this).find('.chicken-box').length != 0){
						$(this).addClass('canUp');
					}
				});
				this.hide_shade();
			},
			
			//弹窗元素临时控制
			'click i.close': function (e) {
				this.hide_shade(e);
			},
			'click section.shade .notice li': function (e) {
				var _this = $(e.currentTarget);
				_this.siblings().removeClass('cur');
				if(_this.hasClass('cur')){
					_this.children('p').slideToggle(200);
				}else{
					_this.children('p').slideDown(200).end().siblings().children('p').slideUp(200);
					_this.addClass('cur');
				}
			},
			//孵化机btn
			'click section.shade .has-mchine button.push': function () {
				this.show_shade('push-egg');
			},
			//收获小鸡
			'click section.shade .has-mchine button.getChick': function (e) {
				var _this = this;
				if($(e.currentTarget).children('i').hasClass('tips')){
					jsonpGet(util.NDNserver+"/getChickenFormSuper",{userId:this.userId,token:this.token},'getChickenFormSuper',$.proxy(function(res){
						console.log('收取孵化机小鸡:',res);
						if(res.code==1){
							_this.show_shade('reap');
							$('.shade .reap p em').text(res.data.superIncubator.chicksIncome.toFixed(1));
							$('section.shade .has-mchine button.getChick i').removeClass('tips');
							_this.RefreshData();
						}else{
							commonAlert_oneBtn(res.msg);
						}
					},this));
				}else{
					commonAlert_oneBtn('没有小鸡可收获');
				}
			},
			//取出小鸡
			'click section.shade .has-mchine button.pull': function () {
				this.show_shade('pull-egg');
			},
			//购买超级孵化机
			'click section.shade .no-mchine #chickBuyMchine': function () {
				this.show_shade('buySuper');
			},
			//超级孵化机放入鸡蛋btn
			'click section.shade .push-egg button:last-of-type': function () {
				var _this = this;
				var egg = $('section.shade .push-egg input').val() -0;
				jsonpGet(util.NDNserver+"/putEggToSuper",{userId: this.userId,token: this.token,eggNum: egg},'putEggToSuper',$.proxy(function(res){
					console.log(res);
					if(res.code==1){
						var bator = res.data.superIncubator;
						_this.initPageData.data.superIncubator = bator;
						$('section.shade .has-mchine .show p:first-of-type em').html(bator.eggNum);
						if(bator.collectFlag && bator.collectFlag == '1'){
							$('.shade .has-mchine button.getChick i').addClass('tips');
						}else{
							$('.shade .has-mchine button.getChick i').removeClass('tips');
						}
						_this.show_shade('has-mchine');
					}else{
						commonAlert_oneBtn(res.msg);
					}
				},this));
			},

			//取消放入鸡蛋
			'click section.shade .push-egg button:first-of-type': function () {
				this.show_shade('has-mchine');
			},

			//取出鸡蛋btn
			'click section.shade .pull-egg button:last-of-type': function () {
				var _this = this;
				var egg = $('section.shade .pull-egg input').val() -0;
				var eggOut=this.initPageData.data.superIncubator.eggNum;
				if(this.initPageData.data.superIncubator.eggNum){
					if(eggOut<egg){
						commonAlert_oneBtn("仅有"+eggOut+"只鸡蛋，不足取出"+egg+"个")
					}else{
						jsonpGet(util.NDNserver+"/getEggFromSuper",{userId: this.userId,token: this.token,eggNum: egg},'getEggFromSuper',$.proxy(function(res){
							console.log(res);
							if(res.code==1){
								var bator = res.data.superIncubator;
								_this.initPageData.data.superIncubator = bator;
								$('section.shade .has-mchine .show p:first-of-type em').html(bator.eggNum);
								if(bator.collectFlag && bator.collectFlag == '1'){
									$('.shade .has-mchine button.getChick i').addClass('tips');
								}else{
									$('.shade .has-mchine button.getChick i').removeClass('tips');
								}
								_this.show_shade('has-mchine');
							}else{
								commonAlert_oneBtn(res.msg);
							}
						},this));
					}
				}
			},
			//取消取出鸡蛋
			'click section.shade .pull-egg button:first-of-type': function () {
				$('section.shade .pull-egg input').val(0)
				this.show_shade('has-mchine');
			},
			//收获小鸡btn
			'click section.shade .reap button': function () {
				$('section.shade .reap input').val(0);
				this.hide_shade();
			},
			'click .friend-list ul button.redListBtn': function(e){
				var userid = $(e.currentTarget).data('id');
				if(userid)
					Backbone.history.navigate('friendFarm?friendId='+userid,{trigger:true});
			},
			'click .storage ul button.fenceLevel':function(){
				Backbone.history.navigate('records',{trigger:true});
			},
			//购买一键打扫
			'click .storage ul li button.keyCleanDay':function(e){
				if($(e.currentTarget).hasClass('redListBtn')){
					this.show_shade('buyKeyCleanDay');
				}
			},
			//取消购买一键打扫
			'click .shade .buyKeyCleanDay button.buffBtn':function(){
				this.show_shade('storage');
			},
			//确认购买一键打扫
			'click .shade .buyKeyCleanDay button.yellowBtn':function(){
				var _this = this;
				jsonpGet(util.NDNserver+"/buyQuickclean",{userId:this.userId,token:this.token},'buyQuickclean',$.proxy(function(res){
					console.log('确认购买一键打扫:',res);
					if(res.code==1){
						_this.gotoLibrary();
						commonAlert_oneBtn('购买成功');
					}else{
						commonAlert_oneBtn(res.msg);
					}
				},this));
			}
		},
		buyDog_prompt:function(){
			this.hide_shade();
			this.confirm_buy_dog()
		},
		quickClean:function () {
			jsonpGet(util.NDNserver+"/quickClean",{userId:this.userId,token:this.token},'quickClean',$.proxy(function(res){
				console.log('一键打扫:',res);
				if(res.code==1){
					commonAlert_oneBtn('打扫成功,收益'+res.data+'只鸡蛋');
				}else{
					commonAlert_oneBtn(res.msg);
				}
			},this));
		},
		initialize : function() {
			this.user=JSON.parse(util.getCookie('user'));
			this.userCookie = JSON.parse(util.getCookie('user'));
			if(!this.user){
				Backbone.history.navigate('loading',{trigger:true});
				return;
			}
			if(this.userCookie && this.userCookie.userStatus == 3){
				Backbone.history.navigate('home',{trigger:true});
				return;
			}
			this.token=this.user.token;
			this.userId=this.user.userId;
			this.template = _.template(template);

			this.render();
		},
		render : function() {
			this.$el.html(this.template());
			//this.adSense()
			this.RefreshData();
			this.setting();
			this.initDataToPage();
			this.click_egg();
		},
        adSense:function () {
            this.$el.append(ad);
            var h = $('#adsBox .adsContent').height();
            var w = $('#adsBox .adsContent').width();
            $('#adsbygoogleStyle').height(h);
            $('#adsbygoogleStyle').width(w);
        },
		initDataToPage: function () {
			var V_this = this;
			var DataRes = JSON.parse(localStorage.getItem('FarmInitPageData'));
			this.initPageData = DataRes;
			
			if(DataRes.data.dog == null){
				$('.chicken-page .dog').hide(0);
			}else {
				$('.chicken-page .dog').show(0);
				document.querySelector('.dog img').src = 'assets/images/farm/dog/0'+DataRes.data.dog.dogLevel+'.gif';
			}
			document.querySelector('.userBox .name').innerText = DataRes.data.loginName;
			//鸡蛋与鸡显示处理
			var totalEgg = DataRes.data.totalEgg;
			var totalChicken = DataRes.data.totalChicken;
			if($(window).width()<375){
				$('.userBox .clm').addClass('round');
				if(totalEgg>9999){
					totalEgg = parseFloat(totalEgg/10000).toFixed(1)+'万';
				}else if(totalEgg>99999){
					totalEgg = parseFloat(totalEgg/10000).toFixed(0)+'万';
				}else if(totalEgg > 999){
					totalEgg = parseFloat(totalEgg).toFixed(0);
				}else{
					totalEgg = parseFloat(totalEgg).toFixed(1);
				}
				if(totalChicken>9999){
					totalChicken = parseFloat(totalChicken/10000).toFixed(1)+'万';
				}else if(totalChicken>99999){
					totalChicken = parseFloat(totalChicken/10000).toFixed(0)+'万';
				}else if(totalChicken>999){
					totalChicken = parseFloat(totalChicken).toFixed(0);
				}else{
					totalChicken = parseFloat(totalChicken).toFixed(1);
				}
			}else{
				totalEgg = parseFloat(totalEgg).toFixed(1);
				totalChicken = parseFloat(totalChicken).toFixed(1);
			}
			document.querySelector('.userBox .clm-l').innerText = totalEgg;
			document.querySelector('.userBox .clm-r').innerText = totalChicken;
			document.querySelector('.notice span').innerText = DataRes.data.notice ? DataRes.data.notice.noticeTitle : '';
			$('.userBox .headImg').css({'background-image': this.user.userPic ? "url('"+this.user.userPic+"')" : "url('assets/images/home/portrait/portrait-man.png')"});

			//围栏
			var fenceLevel = null;
			if(V_this.initPageData.data.fence!=null){
				fenceLevel = V_this.initPageData.data.fence.fenceLevel;
			}
			//孵化机

			//地块处理
			$.each(DataRes.data.landList, function (e, ele) {
				console.log(e,ele);
				var add_chicken = function (n) {
					for(var i= 0; n>i; i++){
						V_this.addChicken(ele.code-1);
					}
				}
				//最多不超过4只
				var chicksNum = ele.chicksNum, maxNum = ele.maxNum;
				if(chicksNum <= maxNum/4){
					add_chicken(1);
				}else if(maxNum/4 < chicksNum && chicksNum <= maxNum/4*2 ){
					add_chicken(2);
				}else if(maxNum/4*2 < chicksNum && chicksNum <= maxNum/4*3){
					add_chicken(3);
				}else if(maxNum/4*3 < chicksNum && chicksNum <= maxNum){
					add_chicken(4);
				}

				var curLi = $('ul.field li').eq(ele.code-1);
				if(fenceLevel != null){
					curLi.addClass('lv'+fenceLevel);
				}
				curLi.data('chicksNum',ele.chicksNum);
				curLi.data('maxNum',ele.maxNum);
				curLi.data('minNum',ele.minNum);
				curLi.data('eggsIncome',ele.eggsIncome);//今日产蛋
				curLi.data('totalIncome',ele.totalIncome);//历史产蛋
				curLi.data('id',ele.id);//地块id
				curLi.data('level',ele.level);//地块类型
				curLi.data('collectFlag',ele.collectFlag)//1为已被收取此时不能被收取，0产生受益可收取鸡蛋，null为一般状态
				//console.log($('ul.field li').eq(ele.code-1).data('chicksNum'))

				if(ele.collectFlag == 0){
					curLi.find('section.Bubble').addClass('egg');
				}else {
					curLi.find('section.Bubble').removeClass('egg');
				}
			})

		},
		RefreshData: function () {
			$('.desktop button').removeClass('cur');
			var _this = this;
			jsonpGet(
				util.NDNserver+'/enterChickenFarm',{userId: _this.userCookie.userId,token: _this.userCookie.token},'enterChickenFarm',
				$.proxy(function (res) {
					console.log("RefreshData",res)
					if(res.code == 1){
						console.log('农场信息刷新',res);
						if(res.data.userMark == 0){
							$('.chicken-page .cock').hide(0);
						}
						if(res.data.userMark == 1){
							$('.chicken-page .cock').show(0);
						}
						localStorage.setItem('FarmInitPageData',JSON.stringify(res));
						_this.initPageData = res;
						//鸡蛋与鸡显示处理
						var totalEgg = res.data.totalEgg;
						var totalChicken = res.data.totalChicken;
			
						if($(window).width()<375){
							$('.userBox .clm').addClass('round');
							if(totalEgg>9999){
								totalEgg = parseFloat(totalEgg/10000).toFixed(1)+'万';
							}else if(totalEgg>99999){
								totalEgg = parseFloat(totalEgg/10000).toFixed(0)+'万';
							}else if(totalEgg > 999){
								totalEgg = parseFloat(totalEgg).toFixed(0);
							}else{
								totalEgg = parseFloat(totalEgg).toFixed(1);
							}
							if(totalChicken>9999){
								totalChicken = parseFloat(totalChicken/10000).toFixed(1)+'万';
							}else if(totalChicken>99999){
								totalChicken = parseFloat(totalChicken/10000).toFixed(0)+'万';
							}else if(totalChicken>999){
								totalChicken = parseFloat(totalChicken).toFixed(0);
							}else{
								totalChicken = parseFloat(totalChicken).toFixed(1);
							}
						}else{
							totalEgg = parseFloat(totalEgg).toFixed(1);
							totalChicken = parseFloat(totalChicken).toFixed(1);
						}
						document.querySelector('.userBox .clm-l').innerText = totalEgg;
						document.querySelector('.userBox .clm-r').innerText = totalChicken;
						//地块信息更新
						$.each(res.data.landList, function (e, ele) {
							console.log(e,ele);

							var curLi = $('ul.field li').eq(ele.code-1);
							//curLi.addClass('lv'+ele.level);
							curLi.data('id',ele.id);
							curLi.data('chicksNum',ele.chicksNum);
							curLi.data('maxNum',ele.maxNum);
							curLi.data('minNum',ele.minNum);
							curLi.data('level',ele.level);//地块类型
							curLi.data('eggsIncome',ele.eggsIncome.toFixed(1));//今日产蛋
							curLi.data('totalIncome',ele.totalIncome.toFixed(1));//历史产蛋
							curLi.data('collectFlag',ele.collectFlag)//1为已被收取此时不能被收取，0产生受益可收取，null为一般状态
							//console.log($('ul.field li').eq(ele.code-1).data('chicksNum'))
							if(ele.collectFlag == 0){
								curLi.find('section.Bubble').addClass('egg');
							}else {
								curLi.find('section.Bubble').removeClass('egg');
							}
							if(ele.limitFlag && ele.limitFlag==1){
								curLi.find('section.Bubble').addClass('notEgg');
							}
							// if(Number(ele.totalIncome)>2700 && ele.maxNum==3000){
							// 	curLi.find('section.Bubble').addClass('notEgg');
                            //
							// }
							// if(Number(ele.totalIncome)>27000 && ele.maxNum==30000){
							// 	curLi.find('section.Bubble').addClass('notEgg');
							// }
						})
					}else if(res.code == -1){

					}
				},this)
			);
		},
		click_egg: function () {
			var V_this = this;
			$('li .Bubble > i').click(function () {
				var parent = $(this).parent();
				var curLi = $(this).parents('li');
				var _this = $(this);
				//console.log('i',event)
				//console.log('V_this.userCookie.userId',V_this.userCookie.userId)
				//console.log("curLi",curLi)
				//console.log("curLi.data('id')",curLi.data('id'))
				//console.log('V_this.userCookie.token',V_this.userCookie.token)
				event.stopPropagation();
				event.cancelBubble = true;

				if(parent.hasClass('egg')){
					jsonpGet(
						util.NDNserver+'/getEggsFromLand',
						{
							userId: V_this.userCookie.userId,
							landId: curLi.data('id'),
							token: V_this.userCookie.token
						},
						'getEggsFromLand',
						$.proxy(function (res) {
							if(res.code == 1){

								$('.chicken-page').append('<div class="eggAnimation"><i></i></div>');
								var dom = document.querySelector('.eggAnimation i');
								dom.style.left = $(this).offset().left+'px';
								dom.style.top = $(this).offset().top+'px';
								curLi.removeClass('egg');
								var goal = document.querySelector('.userBox button.library');
								$(dom).css({
									'transition':'all .6s',
									'-webkit-transition':'all .6s',
									'left': $(goal).offset().left+'px',
									'top': $(goal).offset().top+'px',
									'transform': 'scale(.5,.5)',
									'-webkit-transform': 'scale(.5,.5)',
									'opacity':'0.2'
								})
								curLi.append('<em class="up fadeInUp">+'+ res.data.land.eggsIncome +'</em>');
								setTimeout(function () {
									$('.eggAnimation').remove();
									curLi.find('em.up').remove();
									_this.remove();
								},600);

								V_this.RefreshData();

							}else if(res.code == -1){
								commonAlert_oneBtn('鸡蛋已收取')
							}
						},this)
					);
				}
			})
		},
		setting:function(){
			var _userCookie = this.userCookie;
			//page
			var V_this = this;
			var section = document.querySelector('.userBox section');
			var $_field = $('ul.field li');
			function setSection (){
				section.style.lineHeight = $(section).height()+'px';
				$_field.height($_field.width() *217/230);
				var userAgent = navigator.userAgent.toLowerCase();
				var mobile = userAgent.match(/(iphone|ipod|android|ios|ipad)/i);
				var isPC = !mobile;
				if(isPC){
					$('.fieldBox .middleware').css({'overflow': 'hidden'});
					$('.desktop').width($('#page').width()).css({'margin': 'auto','left': 'initial'});
					$('.floor-bg').width($('#page').width());
					$('section.shade').width($('#page').width()).css({'margin': 'auto','left': 'initial'});
					$('.dog').css({'left': 'initial'});
				}
			};
			document.querySelector('.bg').onload = function () { setSection ();}
			window.onresize = function () { setSection ();}
			$(document).ready(function(){ setSection ();})

			!function dog(){
				var eX,eY;
				var dogX,dogY;
				var touchInnerX,touchInnerY;
				var dog = document.querySelector('.dog');
				var dogWidth = dog.offsetWidth;
				var dogHeight= dog.offsetHeight;
				var dogMaxX,dogMinY=document.querySelector('.vine').offsetHeight,dogMaxY=document.querySelector('.desktop').offsetTop-dogHeight;
				dog.style.top = dogMaxY+'px';

				dog.addEventListener('touchstart',touch, false);
				dog.addEventListener('touchmove',touch, false);
				dog.addEventListener('touchend',touch, false);

				function touch (event){
					var event = event || window.event;

					switch(event.type){
						case "touchstart":
							event.preventDefault();
							eX = event.touches[0].clientX;
							eY = event.touches[0].clientY;
							dogX = dog.offsetLeft;
							dogY = dog.offsetTop;
							touchInnerX = eX-dogX;
							touchInnerY = eY-dogY;
							dogMaxX = $('body').width()-(dogWidth)
							break;
						case "touchend":
							event.preventDefault();
							//event.changedTouches[0].clientX;event.changedTouches[0].clientY;
							if(dog.offsetLeft < ($('body').width()-dogWidth)/2){
								$(dog).animate({'left':0},300);
							}else{
								$(dog).animate({'left':dogMaxX},300);
							}
							break;
						case "touchmove":
							event.preventDefault();
							dogX = event.touches[0].clientX - touchInnerX;
							dogY = event.touches[0].clientY - touchInnerY;

							if(dogX>dogMaxX){
								dog.style.left = dogMaxX+'px';
							}else if(dogX<0){
								dog.style.left = 0;
							}else {
								dog.style.left = dogX+'px';
							}

							if(dogY<dogMinY){
								dog.style.top = dogMinY+'px';
							}else if(dogY>dogMaxY){
								dog.style.top = dogMaxY+'px';
							}else{
								dog.style.top = dogY+'px';
							}

							break;
					}
				}
			}();
			!function cock(){
				var eX,eY;
				var dogX,dogY;
				var touchInnerX,touchInnerY;
				var dog = document.querySelector('.cock');
				var dogWidth = dog.offsetWidth;
				var dogHeight= dog.offsetHeight;
				dog.style.top = "200";
				var dogMaxX,dogMinY=document.querySelector('.vine').offsetHeight,dogMaxY=document.querySelector('.desktop').offsetTop-dogHeight;

				dog.addEventListener('touchstart',touch, false);
				dog.addEventListener('touchmove',touch, false);
				dog.addEventListener('touchend',touch, false);

				function touch (event){
					var event = event || window.event;

					switch(event.type){
						case "touchstart":
							event.preventDefault();
							eX = event.touches[0].clientX;
							eY = event.touches[0].clientY;
							dogX = dog.offsetLeft;
							dogY = dog.offsetTop;
							touchInnerX = eX-dogX;
							touchInnerY = eY-dogY;
							dogMaxX = $('body').width()-(dogWidth)
							break;
						case "touchend":
							event.preventDefault();
							//event.changedTouches[0].clientX;event.changedTouches[0].clientY;
							if(dog.offsetLeft < ($('body').width()-dogWidth)/2){
								$(dog).animate({'left':0},300);
							}else{
								console.log("dogMaxX",dogMaxX)
								dogMaxX=dogMaxX-40;
								$(dog).animate({'left':dogMaxX},300);
							}
							break;
						case "touchmove":
							event.preventDefault();
							dogX = event.touches[0].clientX - touchInnerX;
							dogY = event.touches[0].clientY - touchInnerY;
							if(dogX>dogMaxX){
								dog.style.left = dogMaxX+'px';
							}else if(dogX<0){
								dog.style.left = 0;
							}else {
								console.log("dogX",dogX)
								dog.style.left = dogX+'px';
							}

							if(dogY<dogMinY){
								dog.style.top = dogMinY+'px';
							}else if(dogY>dogMaxY){
								dog.style.top = dogMaxY+'px';
							}else{
								dog.style.top = dogY+'px';
							}

							break;
					}
				}
			}();


			//点击地块
			$('ul.field li').click(function () {
				console.log('li',event)
				event.stopPropagation();
				event.cancelBubble = true;
				var _this = $(this);
				var Type;
				if(_this.hasClass('green')){
					Type = 1;
				}else if(_this.hasClass('gold')){
					Type = 2;
				}
				$('ul.field li').removeClass('clicked');
				_this.addClass('clicked');
				//if可开地 加数字 鸡
				if(_this.hasClass('canOpen')){
					jsonpGet(
						util.NDNserver+'/openChickenHouse',
						{
							userId: _userCookie.userId,
							token: _userCookie.token,
							landNo: _this.index(),
							houseType: Type//绿地是1金地2
						},
						'openChickenHouse',
						$.proxy(function (res) {
							$('ul.field li').removeClass('canOpen');
							if(res.code == 1){//开地扣300小鸡
								console.log('开地返回')
								console.log(res)

								V_this.addChicken(_this.index()-1);
								_this.append('<em class="up fadeInUp">+'+(Type == 1 ? '300' : '3000')+'</em>');
								setTimeout(function () {
									_this.find('em.up').remove();
								},600);
								V_this.RefreshData();
							}else  if(res.code == -1){//仓库小鸡数量不够
								V_this.show_shade('NoChick');
							}
						},this)
					);

				//有鸡地块 动态添加详情
				}else if(_this.find('.chicken-box').length != 0 && !_this.hasClass('canUp')){

					_this.find('.Bubble').append(
						'<div class="clickShowBox show">'+
						'<p><b>小鸡总数:</b><em>'+_this.data('chicksNum')+'</em></p>'+
						'<p><b>本日产蛋:</b><em>'+_this.data('eggsIncome')+'</em></p>'+
						'<p><b>历史产蛋:</b><em>'+_this.data('totalIncome')+'</em></p>'+
						'<i></i>'+
						'</div>'
					);
					if(_this.index() < 4)$('.Bubble .clickShowBox').css({'top': '0'});
					$('body').append(
						'<section class="clickShowBox-shade"></section>'
					);
					$('.clickShowBox-shade').click(function () {
						$(this).remove();
						_this.find('.clickShowBox').remove();
					})
				//if可增养
				}else if(_this.hasClass('canUp')){
					$('ul.field li').removeClass('canUp');
					//增养弹窗
					if($('.desktop .cur').hasClass('Up')){
						jsonpGet(util.NDNserver+'/queryMyStore',{userId: _userCookie.userId, status:'0',//有无更新图标
							token: _userCookie.token},'queryMyStore', $.proxy(function (res) {

								var libraryChicken = res.data.chicksNum;	//仓库里的小鸡数
								var landChicken = _this.data('chicksNum');
						
								var maxNum = _this.data('maxNum');
								V_this.show_shade('Up');
								var num = 1*maxNum - Math.ceil(landChicken);
								var text =0;
								if(num > 0){
									if(Math.floor(libraryChicken) - num > 0){
										text = num;
									}else{
										text = Math.floor(libraryChicken);
									}
								}
								$('.shade .Up input').val(text);
							},this));
					//孵化弹窗
					}else if($('.desktop .cur').hasClass('hatch')){
						jsonpGet(util.NDNserver+'/queryMyStore',{userId: _userCookie.userId, status:'0',//有无更新图标
							token: _userCookie.token},'queryMyStore', $.proxy(function (res) {
								var libraryEgg = res.data.eggsNum;	//仓库里的鸡蛋数
								var landChicken = _this.data('chicksNum');

							
								var maxNum = _this.data('maxNum');
								V_this.show_shade('hatch');
								var num = 1*maxNum - Math.ceil(landChicken);
								var text =0;
								if(num > 0){
									if(Math.floor(libraryEgg) - num > 0){
										text = num;
									}else{
										text = Math.floor(libraryEgg);
									}
								}
								$('.shade .hatch input').val(text);
							},this));
					//收获弹窗
					}else if($('.desktop .cur').hasClass('harvest')){
						jsonpGet(
							util.NDNserver+'/putChickenToStore',
							{
								userId: _userCookie.userId,
								landId: _this.data('id'),
								token: _userCookie.token
							},
							'putChickenToStore',
							$.proxy(function (res) {
								if(res.code == 1){
									//显示收获小鸡数量
									console.log('收获确定返回',res.data)
									V_this.show_shade('reap');
									$('section.shade .reap p em').html(_this.data('chicksNum') - res.data.land.chicksNum);
									_this.data('chicksNum',res.data.land.chicksNum);
									_this.find('.chicken-box').each(function (index, ele) {
										if(index != 0){
											$(this).remove();
										}
									});
									V_this.RefreshData();
								}
							},this)
						)
					}
					//弹窗

					_this.removeClass('canUp');
				}
			});

			//增养确定
			$('section.shade .Up .yellowBtn').click(function () {
				var _clicked = $('ul.field li.clicked');	//选中的地块
				var inputNum = 1*$.trim( $('section.shade .Up input').val());	//填入的小鸡的数量
				var curNum = parseInt( _clicked.data('chicksNum') );
				var maxNum = parseInt( _clicked.data('maxNum') );
				var ui_chickNum = _clicked.find('.chicken-box').length;
				console.log('_clicked',_clicked)
				console.log('inputNum',inputNum)
				console.log('curNum',curNum)
				console.log('inputNum + curNum',inputNum + curNum)
				console.log('maxNum',maxNum)
				console.log('ui_chickNum',ui_chickNum)

				//限制条件 不超总量
				if(inputNum%1 == 0 && (inputNum + curNum) <= maxNum && inputNum > 0){
					//增养成功后需要更新地块点击信息
					jsonpGet(
						util.NDNserver+'/putChickenToLand',
						{
							userId: _userCookie.userId,
							token: _userCookie.token,
							landId: _clicked.data('id'),
							chicksNum: inputNum,//手动输入的增养数量，范围在地块属性里
						},
						'putChickenToLand',
						$.proxy(function (res) {
							if(res.code == 1){
								console.log('增养确定返回',res.data)
								V_this.hide_shade();
								var add_chicken = function (n) {
									for(var i= 0; n>i; i++){
										V_this.addChicken(_clicked.index()-1);
									}
								}
								//最多不超过4只
								var chicksNum = inputNum + curNum;
								if(chicksNum < maxNum/4){
									add_chicken(1 - ui_chickNum);
								}else if(maxNum/4 < chicksNum && chicksNum <= maxNum/4*2 ){
									add_chicken(2 - ui_chickNum);
								}else if(maxNum/4*2 < chicksNum && chicksNum <= maxNum/4*3){
									add_chicken(3 - ui_chickNum);
								}else if(maxNum/4*3 < chicksNum && chicksNum <= maxNum){
									add_chicken(4 - ui_chickNum);
								}

								_clicked.append('<em class="up fadeInUp">+'+inputNum+'</em>');
								setTimeout(function () {
									_clicked.find('em.up').remove();
								},600);

								$('ul.field li').removeClass('clicked');

								V_this.RefreshData();
							}else if(res.code == -1){
								V_this.show_shade('NoChick');
							}
						},this)
					);
				}else {
					var yxj = _clicked.data('maxNum')-curNum;
					var amsg = '';
					if(yxj == 0){
						amsg = '该地已养满小鸡';
					}else if(isNaN(inputNum)){
						amsg = '请输入大于0的整数';
					}else if(inputNum<=0){
						amsg = '数量必须大于0';
					}else if(inputNum%1 != 0){
						amsg = '请输入大于0的整数';
					}else{
						amsg = '最多可增养' + yxj + '只';
					}
					$('section.shade .Up').append(
						'<i style="position:absolute;top:1rem;width:100%;left:0;color:#ff6363;font-size:0.9rem;" '+
						'class="fadeInUp">'+ amsg +'</i>'
					);
					setTimeout(function () {
						$('section.shade .Up .fadeInUp').remove();
					},3000);
				}
			});
			//增养取消
			$('section.shade .Up .buffBtn').click(function () {
				$('section.shade .Up input').val(0);
				V_this.hide_shade();
				$('ul.field li').removeClass('clicked');
			});

			//孵化确认
			$('section.shade .hatch .yellowBtn').click(function () {
				var _clicked = $('ul.field li.clicked');
				var inputNum = 1*$.trim($('section.shade .hatch input').val());
				var curNum = parseInt( _clicked.data('chicksNum') );
				var maxNum = parseInt( _clicked.data('maxNum') );
				var libraryEgg;
				var ui_chickNum = _clicked.find('.chicken-box').length;

				jsonpGet(util.NDNserver+'/queryMyStore',{userId: _userCookie.userId, status:'0',//有无更新图标
					token: _userCookie.token},'queryMyStore', $.proxy(function (res) {
					console.log(res);
					if(res.code == 1){
						 libraryEgg = res.data.eggsNum;	//仓库里的鸡蛋数
						console.log('inputNum',inputNum)
						console.log('curNum',curNum)
						console.log('maxNum',maxNum)
						console.log('libraryEgg',libraryEgg)
						//限制条件 不超仓库鸡蛋
						if(inputNum%1 == 0 && (inputNum+curNum)<= maxNum && inputNum <= libraryEgg && inputNum > 0){
							jsonpGet(
								util.NDNserver+'/incubateEgg',
								{
									userId: _userCookie.userId,
									landId: _clicked.data('id'),
									eggNum: $('section.shade .hatch input').val()-0,//鸡蛋从仓库中扣除
									token: _userCookie.token
								},
								'incubateEgg',
								$.proxy(function (rhx) {
									if(rhx.code == 1){
										console.log('孵化确定返回',rhx.data)
										V_this.hide_shade();
										var add_chicken = function (n) {
											for(var i= 0; n>i; i++){
												V_this.addChicken(_clicked.index()-1);
											}
										}
										//最多不超过4只
										var chicksNum = rhx.data.land.chicksNum;
										if(chicksNum < maxNum/4){
											add_chicken(1 - ui_chickNum);
										}else if(maxNum/4 < chicksNum && chicksNum <= maxNum/4*2 ){
											add_chicken(2 - ui_chickNum);
										}else if(maxNum/4*2 < chicksNum && chicksNum <= maxNum/4*3){
											add_chicken(3 - ui_chickNum);
										}else if(maxNum/4*3 < chicksNum && chicksNum <= maxNum){
											add_chicken(4 - ui_chickNum);
										}

										_clicked.append('<em class="up fadeInUp">+'+(rhx.data.land.chicksNum - curNum)+'</em>');
										setTimeout(function () {
											_clicked.find('em.up').remove();
										},600);

										$('ul.field li').removeClass('clicked');

										V_this.RefreshData();
									}
								},this)
							)
						}else{
							var yxj = maxNum - curNum;
							var amsg = '';
							if(yxj == 0){
								amsg = '该地已养满小鸡';
							}else if(isNaN(inputNum)){
								amsg = '请输入孵化的数量';
							}else if(inputNum%1 != 0){
								amsg = '请输入大于0的整数';
							}else if(inputNum > libraryEgg){
								amsg = '超出仓库鸡蛋数量了';
							}else{
								amsg = '最多可孵化' + yxj + '只';
							}
							$('section.shade .hatch').append(
								'<i style="position:absolute;top:1rem;width:100%;left:0;color:#ff6363;font-size:0.9rem;" '+
								'class="fadeInUp">'+ amsg +'</i>'
							);
							setTimeout(function () {
								$('section.shade .hatch .fadeInUp').remove();
							},2000)
						}
					}
				},this))



			});
			//孵化取消
			$('section.shade .hatch .buffBtn').click(function () {
				$('section.shade .hatch input').val(0);
				V_this.hide_shade();
				$('ul.field li').removeClass('clicked');
			});



			//button菜单栏
			$('.desktop button').click(function () {
				if($(this).hasClass('cur')){
					$(this).removeClass('cur');
					$('ul.field li').removeClass('canUp canOpen ');
					return;
				}
				var _this = $(this);
				_this.addClass('cur').siblings().removeClass('cur');
				//显示可开地
				if(_this.hasClass('reclaim')){
					$('ul.field li').removeClass('canUp');
					$('ul.field li').each(function () {
						if($(this).find('.chicken-box').length == 0){
							$(this).addClass('canOpen');
						}
					});
				//显示可增养 || 孵化
				}else if(_this.hasClass('Up') || _this.hasClass('hatch')){
					$('ul.field li').removeClass('canOpen canUp');
					setTimeout(function () {
						$('ul.field li').each(function (index, ele) {
							if($(this).find('.chicken-box').length != 0){
								$(this).addClass('canUp');
							}
						});
					},200)
				//显示可收获
				}else if(_this.hasClass('harvest')){

					$('ul.field li').removeClass('canOpen canUp');
					$('ul.field li').each(function (index, ele) {
						//产生受益可收取
						if($(this).find('.chicken-box').length != 0 && $(this).data('chicksNum') > $(this).data('minNum')){
							$(this).addClass('canUp');
						}
					});
				//孵化机
				}else{
					if(V_this.initPageData.data.superIncubator instanceof Object){
						V_this.show_shade('has-mchine');
						var mchineData = V_this.initPageData.data.superIncubator;
						$('.shade .has-mchine .show p em').text(mchineData.eggNum);
						if(mchineData.collectFlag && mchineData.collectFlag == '0'){
							$('.shade .has-mchine button.getChick i').addClass('tips');
						}else{
							$('.shade .has-mchine button.getChick i').removeClass('tips');
						}
					}else{
						V_this.show_shade('no-mchine');
					}
				}
			});
		},
		getDate:function (url) {
			jsonpGet(util.NDNserver+"/"+url,{
				userId:this.userId,
				token:this.token},url,$.proxy(function(res){
				console.log(res);
				if(res.code==1){
					var item='';
					var data=res.data;
					var info_the='assets/images/farm/info_then.png';
					var phone_then='assets/images/farm/phone_then.png';
					var heder_then='assets/images/farm/heder_then.png';
					_.each(data,function(obj,index){
						if(!obj.userPic){
							obj.userPic='assets/images/home/portrait/portrait-man.png';
						}
						if(obj.userName){
							if(obj.userName.length>3)
								obj.userName=obj.userName.substr(0,3);
						}
						if(obj.loginName){
							if(obj.loginName.length>3)
								obj.loginName=obj.loginName.substr(0,3);
						}
						var stop_Clean="";
						if(obj.cleanNum){
							stop_Clean='<img style="width: 45%" src="assets/images/farm/stop_Clean.png">'
						}
						item+='<li style="height: 5.555556rem;">'+
							'<div class="userBox" style="background-image: url('+obj.userPic+');top:29%"></div>'+
							'<div class="info" style="height: 50%;">'+
							'<span class="name">'+(obj.loginName ? obj.loginName : '')+stop_Clean+'</span>'+
							'<p><i class="icon"></i><em>'+(obj.eggsIncome ? obj.eggsIncome : 0)+'只</em></p> </div>'+
							'<button style="top: 29%;" data-id="'+obj.userId+'" class="redListBtn"><span>去拜访</span></button> ' +
							'<div style="position: absolute;top: 74%;font-size: 0.7rem;">' +
							'<b><img style="vertical-align: middle; width: 22%;" src="'+info_the+'"></b>' +
							'<b style="-webkit-border-radius: 100px; -webkit-box-sizing: border-box;-moz-border-radius: 100px;background: #fffae4;border-radius: 100rem;padding: 0.15rem 0.15rem;color: #864714;margin-left: 0.3rem; box-sizing: border-box;"><img style="vertical-align: middle; width: 7%;" src="'+heder_then+'"><strong style="margin-left: 0.1rem;">'+obj.userName+'</strong></b>' +
							'<b style="-webkit-border-radius: 100px; -webkit-box-sizing: border-box;-moz-border-radius: 100px;background: #fffae4;border-radius: 100rem;padding: 0.15rem 0.1rem;color: #864714;margin-left: 0.3rem;"><img style="vertical-align: middle; width: 7%;" src="'+phone_then+'"><strong style="margin-left: 0.1rem;">'+obj.phone+'</strong></b></div>' +
							'</li>';
					});
					$('.friend-list ul').html(item);
				}
			},this));
		},
		gotoLibrary: function () {
			this.RefreshData();//重新获得小狗等信息
			var _userCookie = this.userCookie;
			var DataRes = this.initPageData;
			if(DataRes.data.dog == null){
				$('.library-dog button span').html('购买');
			}else {
				$('.library-dog button span').html('升级');
			}
			$('.userBox button.library').removeClass('tips');
			jsonpGet(
				util.NDNserver+'/queryMyStore',{
					userId: _userCookie.userId,
					status:'0',//有无更新图标
					token: _userCookie.token
				},
				'queryMyStore',
				$.proxy(function (res) {
					console.log('gotoLibrary',res)
					if(res.code ==1){
						this.show_shade('storage');
						var _storage = $('.storage');
						 _storage.find('em.chicksNum').html(res.data.chicksNum);
						 _storage.find('em.eggsNum').html(res.data.eggsNum);
			

						if(res.data.fenceLevel){
							_storage.find('p.fenceLevel').html('木栅栏Lv.'+res.data.fenceLevel);
							_storage.find('button.fenceLevel span').html('升级');
						}else {//0
							_storage.find('p.fenceLevel').html('木栅栏');
							_storage.find('button.fenceLevel span').html('获得');
						}

						if(res.data.dogLevel){
							_storage.find('em.dogLevel').html('Lv.'+res.data.dogLevel);
						}else {//0
							_storage.find('em.dogLevel').empty();
						}

						if(res.data.haveSuperIncubator ==1){
							_storage.find('li.haveSuperIncubator button').removeClass('redListBtn').addClass('grayListBtn').find('span').html('已获得');
						}
						if(res.data.keyCleanDay > 0){
							_storage.find('button.keyCleanDay span').removeClass('redListBtn').addClass('grayListBtn').html('剩余'+res.data.keyCleanDay+'日');
						}else{
							_storage.find('button.keyCleanDay').removeClass('grayListBtn').addClass('redListBtn').children('span').text('购买');
						}
					}
				},this)
			);
		},
		confirm_buy_dog:function(){
			event.stopPropagation();
			var _this = this;
			var DataRes = this.initPageData;
			//console.log('小狗等级',_this.initPageData.data.dog.dogLevel)

			//没有小狗,购买
			if(DataRes.data.dog == null){
				_this.show_shade('UpDog');
				$('.UpDog p span').hide(0);
				//等级最高无法升级
			}else if(_this.initPageData.data.dog.dogLevel == 9){
				_this.show_shade('UpDog');
				$('.library-dog button').html('最高等级');
				//升级小狗
			}else if(1 < _this.initPageData.data.dog.dogLevel <9){



				jsonpGet(
					util.NDNserver+'/isHaveFreeUpdateDog',{
						userId: _this.userCookie.userId,
						farmId:_this.initPageData.data.farmId,
						dogId:_this.initPageData.data.dog.id,
						token: _this.userCookie.token
					},
					'isHaveFreeUpdateDog',
					$.proxy(function (res) {
						console.log('gotoLibrary',res)
						if(res.code ==1) {
							_this.show_shade('UpDog');
							$('.UpDog p span').show(0);
							$('.UpDog p .chicken-num').text((_this.initPageData.data.dog.dogLevel+1)*100)
							if(res.data==1){
								$('.UpDog p .chicken-num').text(0)
							}
							switch (_this.initPageData.data.dog.dogLevel){
								case 1:
									$('.UpDog em.odds').html('80%');
									break;
								case 2:
									$('.UpDog em.odds').html('70%');
									break;
								case 3:
									$('.UpDog em.odds').html('60%');
									break;
								case 4:
									$('.UpDog em.odds').html('50%');
									break;
								case 5:
									$('.UpDog em.odds').html('40%');
									break;
								case 6:
									$('.UpDog em.odds').html('30%');
									break;
								case 7:
									// $('.UpDog p .chicken-num').text((_thihis.initPageData.data.dog.dogLevel+1)*100)
									$('.UpDog p .text_opstion').text('升级失败小狗将退回到Lv.1')
									$('.UpDog em.odds').html('20%');
									break;
								case 8:
									$('.UpDog p .text_opstion').text('升级失败小狗将退回到Lv.1')
									// $('.UpDog p .chicken-num').text((_this.initPageData.data.dog.dogLevel+1)*100)
									$('.UpDog em.odds').html('10%');
									break;
							}


						}else{
							commonAlert_oneBtn(res.msg);
						}

					},this))


			}
		},
		show_buy_dog: function (event) {
			event.stopPropagation();
			var _this = this;
			var DataRes = this.initPageData;
			if(DataRes.data.landList.length==0){
				this.show_shade('show_buyDog_prompt');
			}else{
				this.confirm_buy_dog()
			}
		},
		//确定升级
		buy_dog: function () {
			var _userCookie = this.userCookie;
			var _this = this;

			var argument = {
				userId: _userCookie.userId,
				farmId:_this.initPageData.data.farmId,
				token:_userCookie.token
			};

			if(_this.initPageData.data.dog && _this.initPageData.data.dog.dogLevel){
				argument.dogId =  _this.initPageData.data.dog.id;
			}
			console.log('入参',argument);
			jsonpGet(util.NDNserver+'/operateDog',argument,'operateDog',
				$.proxy(function (res) {
					console.log('buy_dog',res);
					if(res.code == 1){
						if(_this.initPageData.data.dog && _this.initPageData.data.dog.dogLevel){
							switch (res.data.dogLevel){
							case _this.initPageData.data.dog.dogLevel:
								commonAlert_oneBtn('很可惜，升级失败，再来一次吧');
								break;
							case  _this.initPageData.data.dog.dogLevel+1:
								commonAlert_oneBtn('升级成功');
								document.querySelector('.dog img').src = 'assets/images/farm/dog/0'+res.data.dogLevel+'.gif';
								$('.storage .library-dog p.name em').text('Lv.'+ res.data.dogLevel);
								break;
							}
						}else{
							commonAlert_oneBtn('购买成功');
							document.querySelector('.dog img').src = 'assets/images/farm/dog/01.gif';
							$('.chicken-page .dog').show();
							$('.storage .library-dog p.name em').text('Lv.1');
						}
						_this.gotoLibrary();
					}else if(res.code == -1){
						_this.hide_shade();
						commonAlert_oneBtn(res.msg);
						$('.Alert-oneBtn button').click(function () {
							_this.show_shade('storage');
						});
					}
				},this)
			);
		},
		buySuper: function () {
			if($('.storage li.haveSuperIncubator button').hasClass('redListBtn')){//未有孵化机
				this.show_shade('no-mchine');
			}
		},
		showNoMchine:function (){
			this.show_shade('no-mchine');
		},
		//确认购买超级孵化机
		buySuper_sure: function () {
			var _this = this;
			var _userCookie = this.userCookie;
			jsonpGet(
				util.NDNserver+'/buySuper',{userId: _userCookie.userId,token: _userCookie.token},'buySuper',
				$.proxy(function (res) {
					if(res.code == 1){
						_this.gotoLibrary();
						commonAlert_oneBtn('购买成功');
					}else {
						commonAlert_oneBtn(res.msg);
					}
				},this)
			)
		},
		hide_shade: function (e) {
			$('section.shade').hide(0);
			$('section.shade >div').hide(0);
			if(e && $(e.currentTarget).parent().hasClass('mchine')){
				$('.desktop .machine').removeClass('cur');
			}
		},
		show_shade: function (ele) {
			$('.desktop button').removeClass('cur');
			$('section.shade').show(0);
			$('section.shade >div').hide(0);
			$('section.shade > .'+ele).show(0);
		},
		addChicken: function (index) {
			var addChicken = {
				init: function (index) {
					if(parseInt(Math.random()*2)){
						$('ul.field li').eq(index).append(
							'<div class="chicken-box"><i class="chicken-run"></i></div>');
						this.move(index);
					}else {
						$('ul.field li').eq(index).append(
							'<div class="chicken-box"><i class="chicken-walk"></i></div>');
						this.move(index);
					}
				},
				move: function (i) {
					if($('ul.field li').eq(i).find('i').eq(-1).hasClass('chicken-run')){
						$('ul.field li').eq(i).find('.chicken-box').eq(-1).addClass('run-move');
					}else{
						$('ul.field li').eq(i).find('.chicken-box').eq(-1).addClass('walk-move')
							.css({
								'left':Math.random()*4.6+'rem'
							})
					}
					var n = parseInt(Math.random()*6);
					$('ul.field li').eq(i).find('.chicken-box').eq(-1).css({
						'animation-delay': -n+'s',
						'top': Math.random()*3.3-1+'rem'
					})
					$('ul.field li').eq(i).find('i').eq(-1).css({
						'animation-delay': -n+'s'
					})

					this.lay(i);
				},
				lay: function (i) {
					var tops = [];
					//var maxIndex = tops.indexOf(Math.max.apply(Math, tops));
					$('ul.field li').eq(i).find('.chicken-box').each(function (index, ele) {
						tops[index] = ele.style.top.replace('rem','')*100;//100扩大精度
					});

					var Max, Min;
					var Max_index, Min_index;
					function MaxAndMin(arr){
						Max = arr[0];
						Min = Max;
						for(var i in arr){
							if(arr[i]>Max){
								Max = arr[i];
								Max_index = i;
							}
						}
						for(var p in arr){
							if(arr[p]<Min){
								Min = arr[p];
								Min_index = p;
							}
						}
					};

					MaxAndMin(tops);

					if(tops.length != 1){
						$('ul.field li').eq(i).find('.chicken-box').css({'z-index':'2'});
						$('ul.field li').eq(i).find('.chicken-box').eq(Min_index).css({'z-index':'1'});
						$('ul.field li').eq(i).find('.chicken-box').eq(Max_index).css({'z-index':'3'});
					}
				}
			}
			addChicken.init(index);
		}
	});
	return View;
});